<?php

namespace App\Kuafu\Config\Validate;

use App\Kuafu\Config\Enum\ConfigEnum;
use App\Kuafu\Config\Model\ConfigModel;
use Illuminate\Http\Request;
use App\Kuafu\Base\Validate\BaseValidate;

class ConfigValidate extends BaseValidate
{
    public static function addConfig(Request $request)
    {
        $rules = [
            'name'   => 'required|string|max:30',
            'key'    => [
                'required',
                'string',
                'max:30',
                function ($attribute, $value, $fail) use ($request) {
                    $count = ConfigModel::query()
                                        ->where('type', (string)$request->post('type'))
                                        ->where('key', (string)$request->post('key'))
                                        ->count();
                    if ($count) {
                        $fail('type、key已存在');
                    }
                }
            ],
            'value'  => 'required|string|max:200',
            'type'   => 'required|string|max:30',
            'remark' => 'nullable|string|max:30',
        ];

        $messages = [];

        return self::validateCheck($request->all(), $rules, $messages);
    }

    public static function setConfig(Request $request)
    {
        $rules = [
            "configId" => [
                'required',
                'integer',
                'min:0',
                function ($attribute, $value, $fail) use ($request) {
                    $count = ConfigModel::query()
                                        ->where('id', (int)$request->post('configId'))
                                        ->count();
                    if (!$count) {
                        $fail('不存在的configId');
                    }
                }
            ],
            'name'     => 'required|string|max:30',
            'key'      => [
                'required',
                'string',
                'max:30',
                function ($attribute, $value, $fail) use ($request) {
                    $count = ConfigModel::query()
                                        ->where('id', '!=', (int)$request->post('configId'))
                                        ->where('type', (string)$request->post('type'))
                                        ->where('key', (string)$request->post('key'))
                                        ->count();
                    if ($count) {
                        $fail('type、key已存在');
                    }
                }
            ],
            'value'    => 'required|string|max:200',
            'type'     => 'required|string|max:30',
            'remark'   => 'nullable|string|max:30',
        ];

        $messages = [];

        return self::validateCheck($request->all(), $rules, $messages);
    }


    /**
     * 版本更新输入校验
     *
     * @param Request $request
     * @return string
     */
    public static function versionUpdateInputCheck(Request $request): string
    {
        $rules = [
            'appType'     => 'required|string|in:' . implode(',', array_keys(ConfigEnum::APP_TYPE)),
            'nowVersion'  => 'required|string',
            'appNameType' => 'required|int|in:' . implode(',', array_keys(ConfigEnum::APP_NAME_TYPE_MAP)),
        ];

        return self::validateCheck($request->all(), $rules, [], [
            "appType"     => "app的平台类型",
            "nowVersion"  => "当前版本号",
            "appNameType" => "app类型"
        ]);
    }
}
